import { _decorator, Component, EventTouch, Input, Label, Node, ProgressBar, Vec3 } from 'cc';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
import { SingleBrick } from './SingleBrick';
import { GAME_TOP } from '../../../Data/GlobalData';
import { BunManger } from '../../../Tool/BunManger';
import { PlateLoad } from '../PlateLoad/PlateLoad';
import { Tools } from '../../../Tool/Tools';
const { ccclass, property } = _decorator;

@ccclass('StickLoad')
export class StickLoad extends BaseComponent {

    //筷子层
    @property(Node)
    private StickLayer: Node = null;

    //按钮
    @property(Node)
    private PosButton: Node = null;

    //选择的筷子
    public ChooseStick: SingleBrick[] = [];

    //盘子层
    @property(Node)
    private PlateLoad: Node = null;

    //筷子数量
    private StickNum: number = 50;
    private StrickMax: number = 50;

    //筷子文本
    @property(Label)
    private StickLabel: Label = null;

    //进度条
    @property(ProgressBar)
    private StickProgress: ProgressBar = null;

    //倒计时文字
    @property(Label)
    private CountDownLabel: Label = null;
    private FixCountDown: number = 1 * 60;

    //游戏开始时间
    private GameStartTime: number = 0;

    protected Init(): void {
        for (let stick of this.StickLayer.children) {
            stick.on(Input.EventType.TOUCH_START, this.StickClick, this);
        }
    }

    protected OnShow(): void {
        this.GameStartTime = Date.now();
        // this.StickNum = this.StrickMax;
        this.UpdateProgress();
    }

    public UpdateLevel(): void {
        // app.topManager.ShowTop(GAME_TOP.NOTOUCH)
        // for (let i = 0; i < this.StickLayer.children.length; i++) {
        //     this.StickLayer.children[i].getComponent(SingleBrick).UpdateStick(BunManger.instance.ReturnStickNum());
        //     this.StickLayer.children[i].getComponent(SingleBrick).PlayAppearAnimation(
        //         this.PosButton.getWorldPosition(),
        //         i,
        //         i == this.StickLayer.children.length - 1
        //     )
        // }
        this.SendStick();
    }

    protected update(dt: number): void {

        this.CountDownLabel.string = Tools.secondsFormat(this.FixCountDown - Math.floor((Date.now() - this.GameStartTime) / 1000), false) + "后可获得10只筷子";

        if (this.FixCountDown - Math.floor((Date.now() - this.GameStartTime) / 1000) <= 0) {
            this.GameStartTime = Date.now();
            //给10只筷子
            this.StickNum += 10;
            this.UpdateProgress();
        }
    }

    /**
     * 更新进度条
     */
    public UpdateProgress() {
        this.StickProgress.progress = Math.min(this.StickNum / this.StrickMax, 1);
        this.StickLabel.string = (this.StickNum > 50 ? (50 + "+" + (this.StickNum - 50)) : this.StickNum) + "/" + this.StrickMax;
    }

    /**
     * 点击筷子
     */
    public StickClick(_e: EventTouch) {
        let stick: SingleBrick = _e.currentTarget.getComponent(SingleBrick);
        if (stick.StickIndex == -1) return;
        stick.IsChoose = !stick.IsChoose;
        let scale_num: number = stick.IsChoose ? 1.2 : 1;
        stick.node.setScale(new Vec3(scale_num, scale_num, scale_num))

        if (stick.IsChoose) {
            this.ChooseStick.push(stick);
        } else {
            this.ChooseStick.splice(this.ChooseStick.indexOf(stick), 1);
        }

        //判断两个是否相同
        if (this.ChooseStick.length == 2) {
            if (this.ChooseStick[0].StickIndex == this.ChooseStick[1].StickIndex) {
                this.PlateLoad.getComponent(PlateLoad).DrawBun(this.ChooseStick[0].StickIndex);
            } else {
                this.StickReset(false);
            }
        }
    }

    /**
     * 筷子恢复
     */
    public StickReset(_change: boolean) {
        for (let stick of this.ChooseStick) {
            stick.UpdateStick(_change ? -1 : stick.StickIndex)
            if (!_change) stick.PlayErrorAni();
        }
        this.ChooseStick = [];
    }

    /**
     * 发筷子
     */
    public SendStick() {
        let display_num: number = 0;
        let play_num: number = 0;
        for (let stick of this.StickLayer.children) {
            if (stick.getComponent(SingleBrick).StickIndex == -1) {
                display_num++;
            }
        }

        for (let stick of this.StickLayer.children) {
            if (stick.getComponent(SingleBrick).StickIndex == -1) {
                if (this.StickNum > 0) {
                    this.StickNum--;
                    this.UpdateProgress();
                    play_num++;
                    stick.getComponent(SingleBrick).UpdateStick(BunManger.instance.ReturnStickNum());
                    stick.getComponent(SingleBrick).PlayAppearAnimation(
                        this.PosButton.getWorldPosition(),
                        play_num,
                        play_num == display_num
                    )
                }
            }
        }
    }

    /**
     * 获取10只筷子
     */
    private GetTenStick() {
        this.StickNum += 10;
        this.UpdateProgress();
    }

    /**
     * 所有筷子随机更换
     */
    private AllStickRandom() {
        for (let i = 0; i < this.StickLayer.children.length; i++) {
            if (this.StickLayer.children[i].getComponent(SingleBrick).StickIndex != -1) {
                BunManger.instance.StickNumArray.push(this.StickLayer.children[i].getComponent(SingleBrick).StickIndex);
                this.StickLayer.children[i].getComponent(SingleBrick).StickIndex = -1;
            }
        };
        this.ChooseStick = [];

        let display_num: number = 0;
        let play_num: number = 0;
        for (let stick of this.StickLayer.children) {
            if (stick.getComponent(SingleBrick).StickIndex == -1) {
                display_num++;
            }
        }

        for (let stick of this.StickLayer.children) {
            if (stick.getComponent(SingleBrick).StickIndex == -1) {
                play_num++;
                stick.getComponent(SingleBrick).UpdateStick(BunManger.instance.ReturnStickNum());
                stick.getComponent(SingleBrick).PlayAppearAnimation(
                    this.PosButton.getWorldPosition(),
                    play_num,
                    play_num == display_num
                )
            }
        }
    }

}


